import { store } from '@/store'
import { defineStore } from 'pinia'
import {  useCache } from '@/hooks/web/useCache'
import * as DeptApi from '@/api/system/dept'

const { wsCache } = useCache()

interface DepartmentVO {
  id: number
  avatar: string
  nickname: string
  deptId: number
}

interface DepartmentInfoVO {
  // USER 缓存
  departments: DeptApi.DeptVO[]
  dictDatas: Map<number, DeptApi.DeptVO>
  deptInfo: DepartmentVO
}

export const useDeptStore = defineStore('admin-dep', {
  state: (): DepartmentInfoVO => ({
    departments: [],
    dictDatas: new Map<number, DeptApi.DeptVO>(),
    deptInfo: {
      id: 0,
      avatar: '',
      nickname: '',
      deptId: 0
    }
  }),
  getters: {
    getDepartments(): DeptApi.DeptVO[] {
      return this.departments
    },
    getDictDatas(): Map<number, DeptApi.DeptVO> {
      return this.dictDatas
    },
  },
  actions: {
   async loadDepts( ){
    if (this.departments.length > 0) {
      return
    }
    this.departments = await DeptApi.getDeptList({})
    this.dictDatas = new Map(this.departments.map(item => [item.id, item]))
   },
    resetState() {
      this.departments = []
      this.deptInfo = {
        id: 0,
        avatar: '',
        nickname: '',
        deptId: 0
      }
    }
  }
})

export const useDeptStoreWithOut = () => {
  return useDeptStore(store)
}
